package com.seasonsfood.mall.web.dispatch.controller.dispatch;

import com.alibaba.dubbo.config.annotation.Reference;
import com.seasonsfood.mall.service.dispatch.api.domain.DispArea;
import com.seasonsfood.mall.service.dispatch.api.service.DispAreaService;
import com.seasonsfood.mall.service.dispatch.api.service.DispSiteService;
import com.seasonsfood.mall.util.constant.ResponseCode;
import com.seasonsfood.mall.util.exception.BizException;
import com.seasonsfood.mall.util.model.JsonResponse;
import com.seasonsfood.mall.util.util.Assert;
import com.seasonsfood.mall.util.util.ResponseUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping("dispArea")
public class DispAreaController {
    @Reference(check = false)
    private DispAreaService dispAreaService;
    @Reference(check = false)
    private DispSiteService dispSiteService;

    @RequestMapping("add")
    public JsonResponse addArea(DispArea dispArea) {
        ResponseCode responseCode = dispAreaService.addDispArea(dispArea);

        if (!ResponseCode.SUCCESS.equals(responseCode)) {
            throw new BizException(responseCode);
        }
        return ResponseUtils.setSuccess(responseCode);
    }

    //根据当前id显示下级区域列表
    @RequestMapping("areaList")
    public JsonResponse areaList(Long id) {

        Assert.notNull(id, "id不能为空");
        return ResponseUtils.setSuccess(dispAreaService.getAreaList(id));
    }


    //返回上一级
    //传递当前级别
    @RequestMapping("backParent")
    public JsonResponse backParent(Integer areaType) {

        Assert.notNull(areaType, "级别不能为空");
        return ResponseUtils.setSuccess(dispAreaService.getParentList(areaType));
    }

    //街道下小区以及注册用户及购买用户
    @RequestMapping("siteList")
    public JsonResponse siteList(Long streetId) {
        return ResponseUtils.setSuccess(dispSiteService.getSiteListByStreetId(streetId));
    }

    //列表模式获得区域树
    @RequestMapping("areaTree")
    public JsonResponse areaTree(Integer rank) {
        if (rank == null) {
            //显示到区
            rank = DispArea.TYPE_COUNTY;
        }
        return ResponseUtils.setSuccess(dispAreaService.getDispAreaTree(rank));
    }

    //列表模式下查找街道
    @RequestMapping("streetList")
    public JsonResponse streetList(String streetName, Integer parentId, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        return ResponseUtils.setSuccess(dispAreaService.getStreetList(streetName, parentId, pageNum, pageSize));
    }

}
